package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.ClassAdmins;
import com.xjc.business.entity.domain.CollageClazzAdminRelations;
import com.xjc.business.entity.domain.CollegeAdmins;
import com.xjc.business.entity.po.classadmin.ClassAdminLogoPo;
import com.xjc.business.entity.po.coladmin.PersonPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.classadmin.ClassAdminLoginVo;
import com.xjc.business.entity.vo.coladmin.PersonVo;
import com.xjc.business.service.ClassAdminsService;
import com.xjc.business.mapper.ClassAdminsMapper;
import com.xjc.business.service.CollageClazzAdminRelationsService;
import com.xjc.business.service.SchoolSetsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import com.xjc.system.utils.BcryptUtil;
import com.xjc.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【class_admins(辅导员表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:31
*/
@Service
public class ClassAdminsServiceImpl extends ServiceImpl<ClassAdminsMapper, ClassAdmins>
    implements ClassAdminsService {

    @Autowired
    @Lazy
    CollageClazzAdminRelationsService collageClazzAdminRelationsService;

    @Autowired
    @Lazy
    SchoolSetsService schoolSetsService;

    /**
     * 分页查询人员信息
     *
     * @param pageSize      每页显示条数
     * @param currentPage   当前页码
     * @param adminUserName 用户名
     * @param adminNickName 昵称
     * @param collegeId
     * @return 信息
     */
    @Override
    public Result<PageVo<PersonVo>> getClassAdminPage(Integer pageSize, Integer currentPage, String adminUserName, String adminNickName, Integer collegeId) {

        LambdaQueryWrapper<CollageClazzAdminRelations> collageClazzAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageClazzAdminRelationsLambdaQueryWrapper.eq(CollageClazzAdminRelations::getColId, collegeId);
        List<CollageClazzAdminRelations> collageClazzAdminRelationsList = collageClazzAdminRelationsService.list(collageClazzAdminRelationsLambdaQueryWrapper);

        List<Integer> collectIdList = collageClazzAdminRelationsList.stream().map(CollageClazzAdminRelations::getClazzAdminId).collect(Collectors.toList());

        PageVo<PersonVo> pageVo = new PageVo<>();

        if(collectIdList.isEmpty()){
            return Result.okWithData(pageVo);
        }

        LambdaQueryWrapper<ClassAdmins> classAdminsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminsLambdaQueryWrapper.like(!adminUserName.isEmpty(), ClassAdmins::getUsername, adminUserName);
        classAdminsLambdaQueryWrapper.like(!adminNickName.isEmpty(), ClassAdmins::getNickname, adminNickName);
        classAdminsLambdaQueryWrapper.in(ClassAdmins::getId, collectIdList);

        Page<ClassAdmins> classAdminsPage = new Page<>(currentPage, pageSize);
        page(classAdminsPage, classAdminsLambdaQueryWrapper);
        List<ClassAdmins> classAdminsPageRecords = classAdminsPage.getRecords();
        List<PersonVo> personVoList = classAdminsPageRecords.stream().map(classAdmins -> {
            PersonVo personVo = new PersonVo();
            personVo.setId(classAdmins.getId());
            personVo.setUsername(classAdmins.getUsername());
            personVo.setNickname(classAdmins.getNickname());
            personVo.setPic(classAdmins.getPic());
            personVo.setPhone(classAdmins.getPhone());
            personVo.setEmail(classAdmins.getEmail());
            personVo.setStatus(MyConstants.USER_STATUS.get(classAdmins.getStatus()));
            personVo.setCreateTime(classAdmins.getCreateTime());
            return personVo;
        }).collect(Collectors.toList());

        pageVo.setCurrent(currentPage);
        pageVo.setPageCount(classAdminsPage.getPages());
        pageVo.setSize(pageSize);
        pageVo.setTotal(classAdminsPage.getTotal());
        pageVo.setData(personVoList);

        return Result.okWithData(pageVo);
    }

    /**
     * 添加管理员
     *
     * @param personPo 管理员信息
     * @return 反馈
     */
    @Override
    public Result<String> addClassAdmin(PersonPo personPo) {
        ClassAdmins classAdmins = new ClassAdmins();
        classAdmins.setUsername(personPo.getUsername());
        classAdmins.setPassword(BcryptUtil.hashPassword(personPo.getPassword()));
        classAdmins.setNickname(personPo.getNickname());
        classAdmins.setPhone(personPo.getPhone());
        classAdmins.setEmail(personPo.getEmail());
        classAdmins.setCreateTime(new Date());
        classAdmins.setUpdateTime(new Date());
        classAdmins.setStatus(1);
        classAdmins.setPic(personPo.getPic());
        save(classAdmins);

        CollageClazzAdminRelations collageClazzAdminRelations = new CollageClazzAdminRelations();
        collageClazzAdminRelations.setClazzAdminId(classAdmins.getId());
        collageClazzAdminRelations.setColId(personPo.getCollegeId());
        collageClazzAdminRelationsService.save(collageClazzAdminRelations);

        return Result.okWithData("增加成功");
    }

    /**
     * 获取学院下的管理员列表
     *
     * @param collegeId 学院id
     * @return 信息
     */
    @Override
    public Result<List<PersonVo>> getPersonList(Integer collegeId) {
        List<PersonVo> personVoList = new ArrayList<>();

        LambdaQueryWrapper<CollageClazzAdminRelations> collageClazzAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageClazzAdminRelationsLambdaQueryWrapper.eq(CollageClazzAdminRelations::getColId, collegeId);
        List<CollageClazzAdminRelations> collageClazzAdminRelationsList = collageClazzAdminRelationsService.list(collageClazzAdminRelationsLambdaQueryWrapper);

        if(collageClazzAdminRelationsList.isEmpty()){
            return Result.okWithData(personVoList);
        }

        List<Integer> collectIdList = collageClazzAdminRelationsList.stream().map(CollageClazzAdminRelations::getClazzAdminId).collect(Collectors.toList());
        LambdaQueryWrapper<ClassAdmins> classAdminsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminsLambdaQueryWrapper.in(ClassAdmins::getId, collectIdList);
        List<ClassAdmins> classAdminsList = list(classAdminsLambdaQueryWrapper);
        for (ClassAdmins classAdmins : classAdminsList) {

            PersonVo personVo = new PersonVo();

            personVo.setId(classAdmins.getId());
            personVo.setNickname(classAdmins.getNickname());
            personVoList.add(personVo);
        }

        return Result.okWithData(personVoList);
    }

    /**
     * 登录
     * @param classAdminLogoPo 登录参数
     * @return 登录成功返回token，失败返回错误信息
     */
    @Override
    public Result<ClassAdminLoginVo> doLogin(ClassAdminLogoPo classAdminLogoPo) {
        String password = classAdminLogoPo.getPassword();
        String username = classAdminLogoPo.getUsername();

        LambdaQueryWrapper<ClassAdmins> classAdminsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminsLambdaQueryWrapper.eq(ClassAdmins::getUsername, username);
        ClassAdmins classAdminsFromDB = getOne(classAdminsLambdaQueryWrapper);

        if (classAdminsFromDB == null) {
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.USERNAME_ERROR);
        }

        if (!(BcryptUtil.checkPassword(password,classAdminsFromDB.getPassword()))){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PASSWORD_ERROR);
        }

        if (!schoolSetsService.sureHasSet(MyConstants.CLASS_ROLE,classAdminsFromDB.getId())) {
            throw new MyException(ResultCode.SUCCESS.getCode(),ResultCode.NOTOPEANSET.getMessage());
        }

        ClassAdminLoginVo classAdminLoginVo = new ClassAdminLoginVo();
        classAdminLoginVo.setId(classAdminsFromDB.getId());
        classAdminLoginVo.setUsername(classAdminsFromDB.getUsername());
        classAdminLoginVo.setNickname(classAdminsFromDB.getNickname());
        classAdminLoginVo.setPic(classAdminsFromDB.getPic());
        classAdminLoginVo.setToken(TokenUtils.generateToken(classAdminsFromDB.getId()));

        LambdaQueryWrapper<CollageClazzAdminRelations> collageClazzAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageClazzAdminRelationsLambdaQueryWrapper.eq(CollageClazzAdminRelations::getClazzAdminId,classAdminsFromDB.getId());

        CollageClazzAdminRelations collageClazzAdminRelationsFromDB = collageClazzAdminRelationsService.getOne(collageClazzAdminRelationsLambdaQueryWrapper);

        if(collageClazzAdminRelationsFromDB !=null){
            classAdminLoginVo.setCollegeId(collageClazzAdminRelationsFromDB.getColId());
        }

        return Result.okWithData(classAdminLoginVo);
    }
}




